<?php
/*
 * @Author: lokei
 * @Date: 2022-09-19 13:45:10
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-05 21:38:24
 * @Description: 
 */
namespace App\Http\Controllers\Cms\Notice;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Cms\Notice\NoticeModel;
use App\Models\Store\StoreModel;
use Illuminate\Http\Request;

class NoticeController extends Controller
{
	public function get(Request $request)
	{
		$notice = NoticeModel::where('id', '=', $request->input('id'))->first();
		$res = ResultTool::success();
		$res['data'] = $notice;
		return $res;
	}
	public function list(Request $request)
	{
		if(!is_null($request->input('status')) && $request->input('status') != '') {
			$items = NoticeModel::where('status', '=', $request->input('status'))->get();
		} else {
			$items = NoticeModel::get();
		}
		$data['items'] = $items;
		$data['count'] = count($items);
		$res = ResultTool::success();
		$res['data'] = $data;
		return $res;
	}
	public function ofStore(Request $request) {
		$conditions = array();
		$conditions['store_id'] = $request->input('store_id');
		if($request->has('status')) {
			$conditions['status'] = $request->input('status');
		}
		$items = NoticeModel::where($conditions)->select(['id', 'title'])->get();
		$data['items'] = $items;
		$data['total'] = count($items);
		$res['data'] = $data;
		return $res;
	}
	public function update(Request $request)
	{
		$status = '0';
		$notice = NoticeModel::where('id', '=', $request->input('id'))->first();
		if(intval($request->input('store_id')) > 0) {
			$store = StoreModel::where('id', '=', $request->input('store_id'))->first();
			$notice->store_id = $store->id;
			$notice->store_name = $store->name;
		} else {
			$notice->store_id = 0;
			$notice->store_name = '平台';
		}
		$notice->title = $request->input('title');
		$notice->content = $request->input('content');
		$notice->status = $request->input('status');
		$notice->save();
		$data['status'] = $status;
		$res['data'] = $data;
		return $res;
	}
	public function create(Request $request)
	{
		$status = '0';
		$notice = new NoticeModel();
		if(intval($request->input('store_id')) > 0) {
			$store = StoreModel::where('id', '=', $request->input('store_id'))->first();
			$notice->store_id = $store->id;
			$notice->store_name = $store->name;
		} else {
			$notice->store_id = 0;
			$notice->store_name = '平台';
		}
		$notice->title = $request->input('title');
		$notice->content = $request->input('content');
		$notice->createtime = date('Y-m-d H:i:s', time());
		$notice->status = $request->input('status');
		$notice->save();
		$data['status'] = $status;
		$res['data'] = $data;
		return $res;
	}
	public function delete(Request $request)
	{
		NoticeModel::where('id', '=', $request->input('id'))->delete();
		$data['status'] = '0';
		$res['data'] = $data;
		return $res;
	}
}
